package com.study.a02mylist;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class A02_ListDemo2 {
    public static void main(String[] args) {


        List<String> list = new ArrayList<>();

        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("5");


        Iterator<String> it = list.iterator();
        while(it.hasNext()){
            String s = it.next();
            System.out.println(s);
        }

        //增强for
        for(String s:list){
            System.out.println(s);
        }

        //lambda
        list.forEach(s-> System.out.println(s));

        //普通for
        for (int i = 0; i < list.size(); i++) {
            String s = list.get(i);
            System.out.println(s);
        }



        //迭代器
        //不能添加
        Iterator<String> it2 = list.iterator();
        while(it2.hasNext()){
            String s = it2.next();
            System.out.println(s);
        }
        //列表迭代器
        //遍历中可以添加元素
        ListIterator<String> listIterator = list.listIterator();
        while(listIterator.hasNext()){
            String s = listIterator.next();
            if("3".equals(s)){
                listIterator.add("333");
            }
            System.out.println(s);
        }


        System.out.println(list);
    }
}
